from django.conf import settings

import locale

from EE.models import Game
from EE.models import Contract

from EE.logic import contract as contract_logic

from django.contrib.auth.models import User

def handle_add_game(user, name, mills):
  game = Game(name=name, turn=0, storm=False)
  game.save()
  game.active_mills = list(set(mills))
  game.save()
  up = User.objects.all().get(id=user)
  up = up.get_profile()
  up.gameControl.add(game)
  up.save()
  return game
  
def handle_end_round(reference_game):
  reference_game.turn = reference_game.turn + 1
  released = []
  for u in getPlayers(reference_game):
    released.append(contract_logic.releaseNewRound(reference_game))
  reference_game.save()
  
def handle_edit_game(game_id, ref_name, ref_mills):
  game = Game.objects.filter(id=game_id).get()
  game.name = ref_name
  game.active_mills = list(set(ref_mills))
  game.save()
  return game
  
def getPlayers(game):
  users = User.objects.all()
  result = []
  for u in users:
    prof = u.get_profile().gamePlay.all()
    if game in prof:
	   result.append(u)
  return result
  
def getMills(game):
  mills = game.active_mills.all()
  return mills
  
def getGame(reference_user):
  game = reference_user.get_profile().gamePlay.all().get()
  return game
